<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>话题采集</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
    body,div,table{
        margin: 0px;
        padding: 0px;
    }
    body{
        padding-top: 40px;
    }
    div.contain{
        /*width: 1024px;
        margin-left:auto;
        margin-right:auto; */
        margin-left:20px;
        
    }
    </style>
</head>
<body>
    <div style="color:red;font-weight: bold;" id='xinxi'></div>
    <br/>
    <div class="contain">
    <form action="fensi.php" method="get">
        来自：<select name='sns'>
          <option value ="weibo" <?php if(isset($_GET['sns'])&&$_GET['sns']=='weibo'){echo 'selected';}?> >微博</option>
          <option value ="renren" <?php if(isset($_GET['sns'])&&$_GET['sns']=='renren'){echo 'selected';}?>>人人</option>
        </select>
        用户：<input type="text" name="uid" width="600px">
        <input type="submit" value="查看粉丝">
    </form>
        分类<select name='cate' class='cate' id='cate'>
          <option value ="chongwu" selected='selected'>宠物</option>
        </select>
        <?php
        if(isset($_GET['zidong'])){
            echo '<input type="hidden" id = "key_key" value="1">';
            echo '<input type="button" id="kaiguan" value="关闭自动">';
        }else{
            echo '<input type="submit" id="tijiao" value="保存到数据库">';
            echo '<input type="hidden" id = "key_key" value="0">';
            echo '<input type="button" id="kaiguan" value="开启自动">';
        }
        ?>
        <br/>
<?php
if(isset($_GET['uid'])&&isset($_GET['sns'])){
    $uid=$_GET['uid'];
    if($_GET['sns'] == 'weibo'){
        if(isset($_GET['page'])){
            $page = $_GET['page'];
        }else{
            $page = 1;
        }
        $last_num = last_num('chongwu',$_GET['sns']);
        $all_num = all_num('chongwu',$_GET['sns']);
        echo '总共：'.$all_num.'个，有'.$last_num.'个没有发送<br/>-----------------------------------<br>';
        if($last_num >= 10000){
            echo '<font color="red" size="36px"><b>微博未发送数量到达一万，停止发送</b></font>';
        }else{
            weibocaiji($uid,$page);
        }
    }elseif($_GET['sns'] == 'renren'){
        if(isset($_GET['page'])){
            $page = $_GET['page'];
        }else{
            $page = 1;
        }
        $last_num = last_num('chongwu',$_GET['sns']);
        $all_num = all_num('chongwu',$_GET['sns']);
        echo '总共：'.$all_num.'个，有<font color="red" size="36px">'.$last_num.'</font>个没有发送<br/>-----------------------------------<br>';
        require_once 'renren2/class/RenrenRestApiService.class.php';
        if($last_num >= 20000){
            echo '<font color="red" size="36px"><b>人人未发送数量到达两万，停止发送</b></font>';
        }else{
            renrencaiji($uid,$page);
        } 
    }
}else{
    echo 'hello';
}

function renrencaiji($uid,$page){
    include_once( 'renren2/class/config.inc.php' );
    $isPublish='1';
    $restApi = new RenrenRestApiService;
    //$token = '233627|6.f9fe1f1a174120526f0605b17cd9dee1.2592000.1389931200-515810977';
    //张永康 1.zyk1750.duapp.com
    $token = '245624|6.9ecb610085d7ebc8a13a073199fa1944.2592000.1390039200-313992699';
    $params = array('page_id'=>$uid,'page'=>$page,'count'=>100,'access_token'=>$token);
    $res1 = $restApi->rr_post_curl('pages.getFansList',$params);

    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $now_domain = 'http://1.zidong1750.duapp.com/fensi_zidong.php?';
    //$now_domain = 'http://test.1.zidong.duapp.com/svn/duapp/1/fensi_zidong.php?';
    echo '<table id="mytable" cellpadding="0" cellspacing="0">';
    if(!isset($res1)){
        echo '<script type="text/javascript">location.href = location.href;</script>';
    }
    print_r($res1);
    $num = count($res1['fans']);
    echo '<tr><td>';
    echo '<a href=fensi.php?sns=renren&uid='.$uid.'&page='.($page-1).'>上一页</a>';
    echo '</td><td>';
    echo '<a href=fensi.php?sns=renren&uid='.$uid.'&page='.($page+1).'>下一页</a>';
    echo '<input type="hidden"  id="zidongdomain" value='.$now_domain.'zidong=1&sns=renren&uid='.$uid.'&page='.$page.' >';
    echo '<input type="hidden"  id="nozidongdomain" value='.$now_domain.'sns=renren&uid='.$uid.'&page='.$page.' >';
    echo '<input type="hidden"  id="zidong_next" value='.$now_domain.'zidong=1&sns=renren&uid='.$uid.'&page='.($page+1).' >';
    echo '<input type="hidden"  id="nozidong_next" value='.$now_domain.'sns=renren&uid='.$uid.'&page='.($page+1).' >';
    echo '</td></tr>';
    for($i = 0;$i<$num;$i++){
        echo '<tr>';
        echo '<td><input type="text" name="screen_name[]" class="screen_name" value="'.$res1['fans'][$i]['name'].'"  style="width:350px" /></td>';
        echo '<td><input type="text" name="user_id[]" class="user_id" value="'.$res1['fans'][$i]['uid'].'"  style="width:350px;margin-left:5px" /><input type="hidden" name="sns[]" class="sns" value="renren" /></td>'; 
        echo '</tr>';
    } 
    echo '</table>';
    echo '</div>';
}
function weibocaiji($uid,$page){
    session_start();
    $show_num =100;
    include_once( 'config.php' ); 
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");

    $name='西电卡卡';
    $appkeydomain = 'alikangkang.com';

    $sql="select * from cai_uid where name='".$name."' and domain='".$appkeydomain."'";
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $token = $row['token'];
    include_once( 'saetv2.ex.class.php' );
    $c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
    $user_message = $c->show_user_by_id($uid);//根据ID获取用户等基本信息
    echo "昵称：".$user_message['screen_name']."<br/>";
    echo "ID：".$user_message['id']."<br/>";
    echo "粉丝数：".$user_message['followers_count']."<br/>";
    if (isset($_REQUEST['cursor'])){
        $cursor = $_REQUEST['cursor'];
    }else{
        $cursor = 0;
    }
    $fensi = $c->followers_by_id($uid,$cursor,$show_num);
    echo '<table id="mytable" cellpadding="0" cellspacing="0">';
    $num = count($fensi['users']);
    echo '<tr><td>';
    if($fensi['previous_cursor']!=0){
        echo '<a href=fensi_zidong.php?sns=weibo&uid='.$uid.'&cursor='.($fensi['previous_cursor']-$show_num).'>上一页</a>';
     }
    echo '</td><td>';
     if($fensi['next_cursor']!=0){
        echo '<a href=fensi_zidong.php?sns=weibo&uid='.$uid.'&cursor='.$fensi['next_cursor'].'>下一页</a>';
        echo '<input type="hidden"  id="next" value=http://1.zidong1750.duapp.com/fensi_zidong.php?sns=weibo&uid='.$uid.'&cursor='.$fensi['next_cursor'].' >';
     }
    echo '</td></tr>';
    for($i = 0;$i<$num;$i++){
        echo '<tr>';
        echo '<td><input type="text" name="screen_name[]" class="screen_name" value="'.$fensi['users'][$i]['screen_name'].'"  style="width:350px" /></td>';
        echo '<td><input type="hidden" name="sns[]" class="sns" value="weibo" /><input type="text" name="user_id[]" class="user_id" value="'.$fensi['users'][$i]['id'].'"  style="width:350px;margin-left:5px" /></td>'; 
        echo '</tr>';
    } 
    echo '</table>';
    echo '</div>';
}
function last_num($cate,$sns){
    include_once( 'config.php' ); 
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="select * from cai_fensi where  done='0' and cate='".$cate."' and sns = '".$sns."'"; 
    $result = mysql_query($sql);
    $num_all = mysql_num_rows($result);
    return $num_all;  
}
function all_num($cate,$sns){
    include_once( 'config.php' ); 
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="select * from cai_fensi where cate='".$cate."' and sns = '".$sns."'"; 
    $result = mysql_query($sql);
    $num_all = mysql_num_rows($result);
    return $num_all;  
}
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"> 
    if($('#key_key').val() == 1){
       benben(); 
    }else{
    $('#tijiao').click(function(){
        benben();
    });
    }
    $('#kaiguan').click(function(){
        if($('#kaiguan').val() == '开启自动'){
            $('#kaiguan').val() == '关闭自动';
            location.href = $('#zidongdomain').val();
        }else{
            $('#kaiguan').val() == '开启自动';
            location.href = $('#nozidongdomain').val();
        }
    });
    
    function benben(){
        var $screen_name=$('.screen_name');
        var $user_id=$('.user_id');
        var $sns=$('.sns');
        var cate=document.getElementById('cate').options[document.getElementById('cate').selectedIndex].value;
        var screen_nameAll = new Array();
        var cateAll = new Array();
        var user_idAll = new Array();
        var snsAll = new Array();
        for(var i=0;i<$user_id.length;i++){
           screen_nameAll.push($screen_name[i].value);
           user_idAll.push($user_id[i].value); 
           cateAll.push(cate);
           snsAll.push($sns[i].value);        
        }
        $.ajax({ 
            type: "post",
            url: "fensi_success.php",
            dataType:"json", 
            data: {screen_name:screen_nameAll,user_id:user_idAll,cate:cateAll,sns:snsAll},
            success: function(data){
                if(data){
                    $('#xinxi').text("共提交"+(data[0]+data[1]+data[2])+"个数据\n成功了"+data[0]+"个\n已经存在了"+data[1]+"个\n失败了"+data[2]+"个");
                    setTimeout(function(){
                        if($('#kaiguan').val() == '开启自动'){
                            location.href = $('#nozidong_next').val();
                        }else{
                            location.href = $('#zidong_next').val();
                        }
                    },2500);
                }
            }
        });
    } 
</script>
</body>
</html>